<?php

namespace app\index\model;

use Firebase\JWT\JWT;
use think\Db;
use think\db\Query;
use think\facade\Request;
use think\facade\Session;
use think\Model;
class Account extends Model{
    protected $table = 'user';
    function chongzhi(){
        $a = Session::get("user");
        $bb = Db::name('user')->where('id',$a['id'])->select();
        $id =$bb[0]['id'];
        $yue=$bb[0]['yue'];
        $input = input();
        $validate = new \app\index\validate\TiCong();
        if(!$validate->check($input)){
            throw new \Exception($validate->getError());
            return;
        }
        $aa=$input['yue'];
        $time = date("Y-m-d H:i:s");
        $da=$aa + $yue;
        Db::name('user')->where('id',$id)->update(['yue'=>$da]);
        $data=['type' => '充值', 'time'=>$time,'yue'=>$aa,'u_id'=>$id];
        Db::name('details')->insert($data);
        return;
    }
    function tixian(){
        $a = Session::get("user");
        $bb = Db::name('user')->where('id',$a['id'])->select();
        $id =$bb[0]['id'];
        $yue=$bb[0]['yue'];
        $input = input();
        $validate = new \app\index\validate\TiCong();
        if(!$validate->check($input)){
            throw new \Exception($validate->getError());
            return;
        }
        $aa=$input['yue'];
        $time = date("Y-m-d H:i:s");
        if ($yue<$aa) {
            throw  new \Exception("余额不足,请重新输入!");
        }else{
            $da=$yue - $aa;
            Db::name('user')->where('id',$id)->update(['yue'=>$da]);
            $data=['type' => '提现', 'time'=>$time,'yue'=>$aa,'u_id'=>$id];
            Db::name('details')->insert($data);
        }
        return;
    }
}
